//
//  HMstatusModel.swift
//  WeiBo
//
//  Created by 李立达 on 15/12/10.
//  Copyright © 2015年 heima11. All rights reserved.
//

import UIKit

class HMstatusModel: NSObject {

    var hmstatus : HMStatus?
    
    var vipImage : UIImage? {
    
        guard let mbrank = hmstatus?.user?.mbrank else{
        
            return nil
        }
        
        if mbrank > 0 && mbrank < 7  {
        
        return UIImage(named: "common_icon_membership_level\(mbrank)")
            
        }
    
    return nil
    }
    
    var verfiedImage : UIImage? {
    
        guard let verfied = hmstatus?.user?.verified else {
            return nil
        }
        
        switch verfied {
        
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return nil
        }
        
    }
    
    var retweetCountStr: String = "转发"
    var commentCountStr: String = "评论"
    var attitudeCountStr: String = "赞"
    var retweetText : String?
    var sourceText: String?
    
    
    /**
    如果今年:
    是否是今天:
    是否在1分钟之内:
    刚刚
    是否在1小时之内:
    xx分钟前
    其他:
    xx小时前
    是否是昨天:
    昨天 10:10
    其他:
    10-11 18:18
    如果不是今年:
    2014-10-11 19:19
    */
    var weiboTimeText : String {
       
        
           guard let creatDate = NSDate.sinaNSdate((hmstatus?.created_at!)!) else
            {
                return ""
            }
        
            return creatDate.sinaDateString 
        
        
//            if isThisYear(creatDate!)
//            {
//                    dt.dateFormat = "是今年"
//                
//                    if calendar.isDateInToday(creatDate!)
//                    {
//                        let result = -creatDate!.timeIntervalSinceNow
//                        
//                        if result < 60
//                        {
//                            dt.dateFormat = "刚刚"
//                        }
//                        else if result < 60 * 60
//                        {
//                            dt.dateFormat = "\(Int(result)/60)分钟前"
//                        }
//                        else
//                        {
//                            dt.dateFormat = "HH:mm"
//                        }
//                        
//                    }
//                    else if calendar.isDateInYesterday(creatDate!)
//                    {
//                            dt.dateFormat = "昨天 HH:mm"
//                }
//            }
//            else
//            {
//                    dt.dateFormat = "yyyy-MM-dd HH:mm:ss"
//            }
    
    }
    
    
    private func isThisYear(date:NSDate)->Bool{
    
    
        let currentDate = NSDate()
        
        let dt = NSDateFormatter()
        
        dt.dateFormat = "yyyy"
        
        dt.locale = NSLocale(localeIdentifier: "en_US")
        
        let dateStr = dt.stringFromDate(date)
        let currentDatestr = dt.stringFromDate(currentDate)
    
        return (dateStr as NSString).isEqualToString(currentDatestr)
    }
    
    
    init(status : HMStatus) {
        super.init()
        self.hmstatus = status
        
        retweetCountStr = getContString((hmstatus?.repost_count)!, defautltitle: "转发")
        commentCountStr = getContString((hmstatus?.comments_count)!, defautltitle: "评论")
        attitudeCountStr = getContString((hmstatus?.attitudes_count)!, defautltitle: "赞")
        
        if let retstatus = hmstatus?.retweeted_status
        {
            if let name = retstatus.user?.name
            {
                retweetText = "@\(name):\(retstatus.text!)"
            }
            
        }
        
        if let source = status.source where source.containsString("\">")
        {
            let startRange = source.rangeOfString("\">")!
            let endRange = source.rangeOfString("</")!
            sourceText = "来自 \(source.substringWithRange(startRange.endIndex..<endRange.startIndex))"
        }
}
    
    
    func getContString(count : Int , defautltitle : String) -> String {
    
    
        if(count<10000)
        {
            return "\(count)"
        }
        else
        {
            let result = CGFloat(count / 1000)/10
            let resultstr = "\(result)万"
            if resultstr.containsString(".0") {
                return resultstr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            return resultstr
        }
    }
    

    
}
